<template>
  <a-drawer
    :title="title"
    width="40%"
    :closable="false"
    :maskClosable="false"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-tabs v-model="tabkey">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文" :forceRender="true"></a-tab-pane>
    </a-tabs>
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules"
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="办事处名称" prop="name" v-if="tabkey == 1">
        <a-input v-model="form.name" placeholder="办事处名称" />
      </a-form-model-item>
      <a-form-model-item label="办事处英文名称" prop="nameEn" v-else>
        <a-input v-model="form.nameEn" placeholder="办事处英文名称" />
      </a-form-model-item> 
      <a-form-model-item label="办事处电话" prop="tel">
        <a-input v-model="form.tel" placeholder="办事处电话" />
      </a-form-model-item> 
      <a-form-model-item label="办事处地址" prop="address" v-if="tabkey == 1">
        <a-textarea
          v-model="form.address"
          placeholder="办事处地址"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>
      <a-form-model-item label="办事英文处地址" prop="addressEn" v-else>
        <a-textarea
          v-model="form.addressEn"
          placeholder="办事英文处地址"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>  
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
  </a-drawer>
</template>

<script>

import { updateOffice, addOffice } from '@/api/manage'
import { showMessage } from '@/utils/mixins'

export default {
  mixins: [showMessage],
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      tabkey: '1',
      title: '',
      visible: false,
      labelCol: {span: 4},
      wrapperCol: {span: 20},
      form: {
        name: undefined,
        nameEn: undefined,
        tel: undefined,
        address: undefined,
        addressEn: undefined
      },
      rules: {
        name: [{required: true, message: '办事处名称不能为空', trigger: 'blur'}],
        nameEn: [{required: true, message: '办事处名称英文不能为空', trigger: 'blur'}],
        tel: [{required: true, message: '办事处电话不能为空', trigger: 'blur'}],
        address: [{required: true, message: '办事处地址不能为空', trigger: 'blur'}],
        addressEn: [{required: true, message: '办事处地址英文不能为空', trigger: 'blur'}],
      },
      id: undefined,
      status: undefined
    }
  },
  methods: {
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          if(this.status == 1) {
            addOffice(this.form).then(res => {
              this.showMessage(res,() => {
                this.onClose()
                this.parent.getList()
              })
            })
          }else {
            updateOffice({id:this.id,...this.form}).then(res => {
              if(res.code == '0') {
                this.showMessage(res,() => {
                  this.onClose()
                  this.parent.getList()
                })
              }
            })
          }
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      this.visible = false
      this.id = undefined
      this.tabkey = '1'
      this.status = undefined
      for(const key in this.form) {
        this.form[key] = undefined
      }
    }
  }
}
</script>

<style lang="less" scoped>
.plus-icon,
.minus-icon {
  font-size: 20px;
  margin-right: 6px;
  float: right;
  color: #F5222D;
  margin-top: 10px;
}
.minus-icon {
  color: #1890ff;
}
</style>